<template>
  <div id="app" :class="classObj" class="app-wrapper">
    <el-scrollbar style="height:100%">
      <div>
        <div :class="{'fixed-header':fixedHeader}">
          <navbar />
          <ad v-if="user.userId" />
        </div>
        <app-main />
        <p class="foot">
          <el-button type="text" disabled>Copyright © {{ copyright }}</el-button>
          <el-button type="text">{{ siteName }} All Rights Reserved.</el-button>
        </p>
        <right-panel v-if="showSettings" :show="showSettings" @close="handleCloseSetting">
          <settings />
        </right-panel>
      </div>
    </el-scrollbar>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getToken, setToken } from '@/utils/auth'
import { Navbar, AppMain, Settings, Ad } from './components'
import RightPanel from '@/components/RightPanel'
import ResizeMixin from './mixin/ResizeHandler'

export default {
  name: 'IndexLayout',
  components: {
    Navbar,
    AppMain,
    RightPanel,
    Settings,
    Ad
  },
  mixins: [ResizeMixin],
  data() {
    return {
      siteName: '',
      copyright: ''
    }
  },
  computed: {
    ...mapState({
      sidebar: state => state.app.sidebar,
      device: state => state.app.device,
      fixedHeader: state => state.settings.fixedHeader,
      user: state => state.user.info
    }),
    showSettings: {
      get() {
        return this.$store.state.settings.showSettings
      },
      set(val) {
        this.$store.dispatch('settings/changeSetting', {
          key: 'showSettings',
          value: true
        })
      }
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        let token = route.query && route.query.token
        token = token || getToken()
        if (token) {
          setToken(token)
          this.loginUser(token)
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.getFooterInfo()
  },
  methods: {
    async loginUser(token) {
      await this.$store.dispatch('user/getInfo')
    },
    getFooterInfo() {
      this.$http.get('/footer/info').then(response => {
        this.siteName = response.param.siteName
        this.copyright = response.param.copyright
      }).catch(e => {})
    },
    handleClickOutside() {
      this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
    },
    handleCloseSetting() {
      this.$store.dispatch('settings/changeSetting', {
        key: 'showSettings',
        value: false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import "~@/styles/mixin.scss";
  @import "~@/styles/variables.scss";

  .app-wrapper {
    @include clearfix;
    position: relative;
    height: 100%;
    width: 100%;
    &.mobile.openSidebar{
      position: fixed;
      top: 0;
    }
  }
  .drawer-bg {
    background: #000;
    opacity: 0.3;
    width: 100%;
    top: 0;
    height: 100%;
    position: absolute;
    z-index: 999;
  }

  .fixed-header {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 9;
    width: 100%;
    transition: width 0.28s;
  }

  .hideSidebar .fixed-header {
    width:100%
  }

  .mobile .fixed-header {
    width: 100%;
  }

  .foot {
    text-align: center;
  }
</style>
